package com.mew.chess.core.game.shbz;


import com.mew.chess.core.common.handler.ChessHandlerMapCmdType;
import com.mew.chess.core.common.handler.ChessPlayAnnotation;
import com.mew.chess.core.common.handler.ChessPlayHandler;
import com.mew.chess.core.game.shbz.game.ShbzGameManager;
import com.mew.chess.core.game.shbz.game.ShbzRoomManager;
import com.mew.chess.core.game.shbz.game.ShbzTableManager;
import com.mew.chess.core.game.shbz.game.common.FishTableUtil;
import com.mew.chess.core.game.shbz.game.common.FishUserBO;
import com.mew.chess.core.game.shbz.game.message.FishCMDMapType;
import com.mew.chess.core.game.shbz.game.message.request.FireRequestMessage;
import com.mew.common.exception.ResponseCode;
import com.mew.common.rest.bo.GameSession;
import com.zebra.remoting.protocol.ZebraCommand;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@ChessPlayAnnotation(ChessHandlerMapCmdType.ShbzFire)
@Service
@Slf4j
public class ShbzFireHandler implements ChessPlayHandler<FireRequestMessage> {


    @Override
    public void handle(ZebraCommand<FireRequestMessage> request, GameSession gameSession) {

        long userId = gameSession.getUserId();
        int roomId = gameSession.getRoomId();
        //todo 子弹验证

        ShbzRoomManager manager = ShbzGameManager.getInstance().getController(roomId);
        ShbzTableManager table = manager.getTableByUserId(userId);
        FireRequestMessage message = request.getRequest();

        if (message.getBetId() < 1 || message.getBetId() > manager.getBets().length) {
            request.sendFailureCode(ResponseCode.BetError.code());
            return;
        }

        int isDouble = request.getRequest().getIsDouble();
        int bet = ShbzGameManager.getInstance().getController(roomId).getBets()[message.getBetId() - 1] * (isDouble + 1);
        FishUserBO fishUserBO = table.getOnlineUserMap().get(userId);
        if (fishUserBO.getGoldNumber() < bet) {
            request.sendFailureCode(ResponseCode.GoldNo.code());
            return;
        }

        FishTableUtil.sendMsgByTable(table, message, FishCMDMapType.HitMessage.code(), 0);
    }
}
